schema = require('async-validator').default;

function handleErrors(errors, fields) {
    console.log(errors)
}

schema.register('test', (rule, value, callback, source, options) => {
    var errors = [];
    if (value != 'test') errors.push('not equal test');

    callback(errors)
})

var descriptor = {
    name: [{
        type: "number",
        required: true,
        min: 1,
        max: 2,
        transform(value) {
            return value * 2;
        },
    }, { type: 'test', asyncValidator: (rule, value) => Promise.reject('test reject!') }],
};
var validator = new schema(descriptor);

validator.validate({
    name: "muji"
}, (errors, fields) => {
    if (errors) {
        // validation failed, errors is an array of all errors
        // fields is an object keyed by field name with an array of
        // errors per field
        return handleErrors(errors, fields);
    }
    // validation passed
})
    .then(() => {
        // validation passed
    })
    .catch(({ errors, fields }) => {
        return handleErrors(errors, fields);
    })

// validator.validate({ name: 4, asyncValidator: (rule, value) => Promise.resolve(value) }, (errors, fields) => {
//     if (errors) {
//         // validation failed, errors is an array of all errors
//         // fields is an object keyed by field name with an array of
//         // errors per field
//         console.log(errors)
//     }
//     console.log('ok')
//     // validation passed
// });